package com.taotao.search.service.impl;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.search.mapper.ItemMapper;
import com.taotao.search.pojo.Item;
import com.taotao.search.service.ItemService;

@Service
public class ItemServiceImpl implements ItemService {
	@Autowired
	private ItemMapper itemMapper;
	@Autowired
	private SolrServer solrServer;
	@Override
	public TaotaoResult importAllItems() throws SolrServerException, IOException {

		
		List<Item> itemList = itemMapper.getItemList();
		
		for(Item item : itemList) {
			SolrInputDocument document = new SolrInputDocument();
			document.addField("id", item.getId());
			document.addField("item_title", item.getTitle());
			document.addField("item_sell_point", item.getSellPoint());
			document.addField("item_price", item.getPrice());
			document.addField("item_image", item.getImage());
			document.addField("item_category_name", item.getCategoryName());
			document.addField("item_desc", item.getItemDesc());
			solrServer.add(document);
		}
		solrServer.commit();
		return TaotaoResult.ok();
	}
	@Override
	public TaotaoResult importItemById(Item item) throws SolrServerException, IOException {
		SolrInputDocument document = new SolrInputDocument();
		document.addField("id", item.getId());
		document.addField("item_title", item.getTitle());
		document.addField("item_sell_point", item.getSellPoint());
		document.addField("item_price", item.getPrice());
		document.addField("item_image", item.getImage());
		document.addField("item_category_name", item.getCategoryName());
		document.addField("item_desc", item.getItemDesc());
		solrServer.add(document);
		solrServer.commit();
		return null;
	}
	
}
